package com.saishi.dao.impl;

import com.saishi.dao.tkxtDao;
import com.saishi.entity.Question;
import com.saishi.util.AliPool;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class tkxtDaoImpl implements tkxtDao{
    //模糊查询
    @Override
    public List selectLike(String name) throws SQLException {
        List list=new ArrayList();
        Connection conn= AliPool.getConn();
        String sql="select * from question where questionname like '%"+name+"%'";
        PreparedStatement pst=conn.prepareStatement(sql);
        ResultSet res=pst.executeQuery();
        while(res.next()){
            int id=res.getInt(1);
            String quname=res.getString(2);
            int sc=res.getInt(3);
            int ty=res.getInt(4);
            String s=res.getString(5);
            String a=res.getString(6);
            Timestamp z=res.getTimestamp(7);
            Question q=new Question(id,quname,sc,ty,s,a,z);
            list.add(q);
        }
        return list;
    }

    //查询单选
    @Override
    public List selectdx(int type) throws SQLException {
        List list=new ArrayList();
        Connection conn= AliPool.getConn();
        String sql="select * from question where questiontype=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1,type);
        ResultSet res=pst.executeQuery();
        while(res.next()){
            int id=res.getInt(1);
            String quname=res.getString(2);
            int sc=res.getInt(3);
            int ty=res.getInt(4);
            String s=res.getString(5);
            String a=res.getString(6);
            Timestamp z=res.getTimestamp(7);
            Question q=new Question(id,quname,sc,ty,s,a,z);
            list.add(q);
        }
        return list;
    }

    //查询多选
    @Override
    public List selectduox(int type) throws SQLException {
        List list=new ArrayList();
        Connection conn= AliPool.getConn();
        String sql="select * from question where questiontype=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1,type);
        ResultSet res=pst.executeQuery();
        while(res.next()){
            int id=res.getInt(1);
            String quname=res.getString(2);
            int sc=res.getInt(3);
            int ty=res.getInt(4);
            String s=res.getString(5);
            String a=res.getString(6);
            Timestamp z=res.getTimestamp(7);
            Question q=new Question(id,quname,sc,ty,s,a,z);
            list.add(q);
        }
        return list;
    }


    //查询填空
    @Override
    public List selecttk(int type) throws SQLException {
        List list=new ArrayList();
        Connection conn= AliPool.getConn();
        String sql="select * from question where questiontype=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1,type);
        ResultSet res=pst.executeQuery();
        while(res.next()){
            int id=res.getInt(1);
            String quname=res.getString(2);
            int sc=res.getInt(3);
            int ty=res.getInt(4);
            String s=res.getString(5);
            String a=res.getString(6);
            Timestamp z=res.getTimestamp(7);
            Question q=new Question(id,quname,sc,ty,s,a,z);
            list.add(q);
        }
        return list;
    }

    //把题目id加入试卷

    @Override
    public void addId(int examid,String list) throws SQLException {
        Connection conn=AliPool.getConn();
        String sql="update exam set examquestionids=? where examid=?";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1,list);
        pst.setInt(2,examid);
        int num = pst.executeUpdate();
    }

    //通过id取出examquestionids来

    @Override
    public String selectById(int examid) throws SQLException {
        String name=null;
        Connection conn=AliPool.getConn();
        String sql="select examquestionids from exam where examid=?";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setInt(1,examid);
        ResultSet res = pst.executeQuery();
        while(res.next()){
            name=res.getString(1);
        }
        return name;
    }

    public static void main(String[] args) throws SQLException {
        String name=new tkxtDaoImpl().selectById(1);
        System.out.println(name);
    }

}
